package com.sporadic.backstage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sporadic.backstage.dto.UserDto;
import com.sporadic.backstage.entity.User;
import com.sporadic.backstage.service.UserService;
import dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin(origins = {"http://localhost:8080"}, methods = {RequestMethod.GET, RequestMethod.POST}, allowCredentials = "true")
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 在指定商家中根据手机号分页查询会员信息
     * @param map (查询条件)
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectUserPageInfo")
    public IPage<UserDTO> selectUserPageInfo(@RequestBody Map<String,Object> map){
        Page<User> page=new Page<>();
        page.setCurrent((Integer)map.get("current"));
        page.setSize((Integer)map.get("pageSize"));
        System.out.println(map);
        return userService.selUserPage(page,map);
    }
    /**
     * 增加会员信息
     * @param user 用户信息详情
     * @return 大于0添加成功  其他失败
     */
    @ResponseBody
    @PostMapping("/addUser")
    public Integer addUser(User user) {
        System.out.println("用户信息======="+user.toString());
        System.out.println("用户名========"+user.getUserName());
        return userService.addUser(user);
    }

    /**
     * 修改会员信息
     * @param user 用户信息详情
     * @return  大于0添加成功  其他失败
     */
    @ResponseBody
    @RequestMapping("/upUser")
    public Integer upUser(User user){
        return userService.upUserByUid(user);
    }
    /**
     * 批量删除会员
     * @param list 要删除会员集合
     * @return
     */
    @ResponseBody
    @RequestMapping("/delUser")
    public int delUser(@RequestParam("list") List list) {
        return userService.delUser(list);
    }
    /**
     * 查询商家指定会员等级总人数
     * @param merchantId 商家编号
     * @param rankId 会员等级编号
     * @return 总人数
     */
    @ResponseBody
    @RequestMapping("/selUserCountByRank")
    public Integer selUserCountByRank(Integer merchantId,Integer rankId){
        return userService.selUserCountByRank(merchantId, rankId);
    }

    /**
     * 查询商家本月消费会员
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/selByMonthUserPage")
    public IPage<User> selByMonthUserPage(@RequestBody Map<String, Object> map) {
        Page<User> page=new Page<>();
        page.setCurrent((Integer)map.get("current"));
        page.setSize((Integer)map.get("pageSize"));
        return userService.selByMonthUserPage(page,(Integer)map.get("merchantId"));
    }
    /**
     * 分页查询商家今天生日会员信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/selByBirthdayNowUserPage")
    public IPage<User> selByBirthdayNowUserPage(@RequestBody Map<String,Object> map) {
        Page<User> page=new Page<>();
        page.setCurrent((Integer)map.get("current"));
        page.setSize((Integer)map.get("pageSize"));
        return userService.selByBirthdayNowUserPage(page,(Integer)map.get("merchantId"));
    }
    /**
     * 分页查询商家本月过生日会员信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("/selByBirthdayMonthUserPage")
    public IPage<User> selByBirthdayMonthUserPage(@RequestBody Map<String,Object> map) {
        Page<User> page=new Page<>();
        page.setCurrent((Integer)map.get("current"));
        page.setSize((Integer)map.get("pageSize"));
        return userService.selByBirthdayMonthUserPage(page,(Integer)map.get("merchantId") );
    }

     /**
     * 查询会员构成
     * @param merchantId 商家ID
     * @return 返回会员构成信息
     */
     @RequestMapping("/findMembershipFormSkip")
     @ResponseBody
     public List<UserDto> findMembershipFormSkip(Integer merchantId){
        return userService.findMembershipFormSkip(merchantId);
    }

}
